package cn.bossx.user.service.impl;

import cn.bossx.basic.constant.BaseConstants;
import cn.bossx.basic.exception.BusinessException;
import cn.bossx.basic.service.impl.BaseServiceImpl;
import cn.bossx.basic.util.MD5Utils;
import cn.bossx.basic.util.StrUtils;
import cn.bossx.org.mapper.EmployeeMapper;
import cn.bossx.user.domain.Logininfo;
import cn.bossx.user.domain.User;
import cn.bossx.user.dto.UserDto;
import cn.bossx.user.mapper.LogininfoMapper;
import cn.bossx.user.mapper.UserMapper;
import cn.bossx.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    //1.基础的CRUD的实现通过继承得到  BaseServiceImpl

    //2.扩展的接口实现自己写
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void phoneRegister(UserDto userDto) {

        // 1.空校验
        if(StringUtils.isEmpty(userDto.getPhone())
                || StringUtils.isEmpty(userDto.getPassword())
                || StringUtils.isEmpty(userDto.getConfirmPassword())
                || StringUtils.isEmpty(userDto.getVerifyCode())
                ){
            throw new BusinessException("参数错误!!!");
        }
        // 2.两次密码是否一致
        if(!userDto.getPassword().equals(userDto.getConfirmPassword())){
            throw new BusinessException("两次密码不一致!!!");
        }
        // 3.用户不能被注册
        User userTmp = userMapper.loadByPhone(userDto.getPhone());
        if (userTmp != null) {
            throw new BusinessException("用户已被注册!!!!");
        }

        // 1.验证码是否过期
        Object codeTmp = redisTemplate.opsForValue().get(BaseConstants.BUSINESS_REGISTER_PREFIX + userDto.getPhone());
        if (codeTmp == null) {
            throw new BusinessException("验证码已失效,请重新获取!");
        }
        // 2.验证码是否正确
        String code = ((String)codeTmp).split(":")[0];
        if(!userDto.getVerifyCode().equalsIgnoreCase(code)){
            throw new BusinessException("验证码错误!!!");
        }
        //三:存数据
        User user = userDto2User(userDto);
        Logininfo logininfo = user2Logininfo(user);
        // 1.保存logininfo
        logininfoMapper.save(logininfo);//返回自增id
        // 2.保存 user
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }

    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        //使用盐值加密
        String md5Pwd = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        // 同名原则拷贝
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);
        return logininfo;
    }
}